<template>
  <div>
    <el-row style="margin-bottom:20px;display:flex;justify-content:space-between">
      <el-input
        placeholder="人员姓名"
        style="width:20%;margin-right:10px"
        v-model="licenseData.hash.username"
        clearable
      ></el-input>
      <el-input
        placeholder="证书编号"
        style="width:20%;margin-right:10px"
        v-model="licenseData.hash.cardnum"
        clearable
      ></el-input>
      <el-input
        placeholder="IC卡号"
        style="width:20%;margin-right:10px"
        v-model="licenseData.hash.icnum"
        clearable
      ></el-input>
      <el-select
        v-model="licenseData.hash.cardtype"
        placeholder="人员类别"
        style="margin-right:10px"
        clearable
      >
        <el-option
          v-for="item in itemdata1"
          :key="item.value"
          :label="item.hash.code_name"
          :value="item.hash.code_id"
        ></el-option>
      </el-select>
      <!--
      <el-select
        v-model="occupation_typeArr"
        placeholder="职业领域"
        style="margin-right:10px"
        clearable
      >
        <el-option
          v-for="item in itemdata2"
          :key="item.value"
          :label="item.hash.code_name"
          :value="item.hash.code_id"
        ></el-option>
      </el-select>
      <el-select v-model="position_typeArr" placeholder="从业岗位" style="margin-right:10px" clearable>
        <el-option
          v-for="item in itemdata3"
          :key="item.value"
          :label="item.hash.code_name"
          :value="item.hash.code_id"
        ></el-option>
      </el-select>
      -->
      <el-date-picker
        v-model="licenseData.hash.starttime"
        style="width:32%;margin-right:20px"
        type="daterange"
        rangeSeparator="至"
        startPlaceholder="开始时间始"
        endPlaceholder="开始时间止"
        valueFormat="yyyy-MM-dd"
        clearable
      ></el-date-picker>
      <el-date-picker
        v-model="licenseData.hash.validtime"
        style="width:32%;margin-right:20px"
        type="daterange"
        rangeSeparator="至"
        startPlaceholder="有效期始"
        endPlaceholder="有效期止"
        valueFormat="yyyy-MM-dd"
        clearable
      ></el-date-picker>
      <el-button size="mini" type="primary" @click="getlicenselist">搜索</el-button>
    </el-row>
    <!-- {{itemvalname}} -->
    <el-row>
      <el-button size="mini" @click="showaddlicense">添加上岗信息</el-button>
      <el-button size="mini" type="primary" @click="amandlicense">编辑</el-button>
      <el-button size="mini" type="success" @click="deletelicense">删除</el-button>
    </el-row>
    <licensetable
      :tableData="licenselist"
      @itemvals="itemvals"
      :pagobj.sync="pagobj"
      :ischeck="false"
      id="tableExcel"
    ></licensetable>
    <addlicense
      :itemdata1="itemdata1"
      :itemdata2="itemdata2"
      :itemdata3="itemdata3"
      v-if="isaddlicense"
      :itemvalname="itemvalname"
      :chousebtn="btnvalue"
    ></addlicense>
  </div>
</template>

<script>
import addlicense from '~/components/person/license/addlicense.vue';
import licensetable from '~/components/person/license/licensetable.vue';
import FileSaver from 'file-saver';
import XLSX from 'xlsx';

export default {
  data() {
    return {
      dialogVisible: false,
      ts: this.newStore(),
      licenseData: {
        cols: [],
        hash: {
          username: '',
          cardnum: '',
          icnum: '',
          cardtype: '',
          occupation_type: '',
          position_type: '',
          intime: '',
          validtime: [],
          starttime: [],
          pid: '',
          id: '',
        },
      },
      pagesize: 0,
      currentPage: 0,
      pagobj: {
        // 分页
        currentPage3: 1,
        pagesize: 10,
        total: 10,
      },
      licenseidarr: [], // 删除所用的数组id
      isaddlicense: false, // 添加弹窗
      // isaddlicense2: false, //编辑弹窗
      btnvalue: 0,
      Object: '',
      itemval: {}, // 编辑传回来的d对象
      cnumber: '',
      licenselist: [],
      itemdata1: [], // 人员类别
      itemdata2: [], // 职业领域
      itemdata3: [], // 岗位资格
      occupation_typeArr: '',
      position_typeArr: '',
      changelist: {},
    };
  },
  components: {
    addlicense,
    licensetable,
  },
  created() {
    this.getlicenselist();
    this.getitem1();
    this.getitem2();
    this.getitem3();
  },
  methods: {
    // 删除上岗证
    deletelicense() {
      // console.log(this.licenseidarr, "0");
      if (this.licenseidarr.length > 1) {
        this.$confirm('是否批量删除上岗证信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const req = {
            handle:
              '1CB43CC1631B5D5B14CBF34BC23B86F231062A3F18BDCAFAF568A6CE28C87E07C0947A4384F3EDEB',
            id: this.licenseidarr,
          };
          this.spost(this.ts, '/mainservlet', req).then((ts) => {
            if (ts.d.sflag == 'success') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
              this.getlicenselist();
            }
          });
        });
      } else if (this.licenseidarr.length == 1) {
        this.$confirm('是否删除该上岗证信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const req = {
            handle:
              '1CB43CC1631B5D5B14CBF34BC23B86F231062A3F18BDCAFAF568A6CE28C87E07C0947A4384F3EDEB',
            id: this.licenseidarr,
          };
          this.spost(this.ts, '/mainservlet', req).then((ts) => {
            if (ts.d.sflag == 'success') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
              this.getlicenselist();
            }
          });
        });
      } else {
        this.$alert('请选择上岗证', '提示');
      }
    },
    // 编辑上岗证
    amandlicense() {
      (this.dialogVisible = true), (this.btnvalue = 2);
      if (Object.keys(this.itemval).length == 1) {
        let itemvals = {};
        itemvals = this.itemval[0];
        this.itemvalname = itemvals;
        // console.log(this.itemvalname,'0123')
        this.reload('isaddlicense');
      } else {
        this.$alert('编辑只能编辑一个', '提示');
      }
    },
    // table 传给父组件
    itemvals(val) {
      // console.log(val, "wwwwww");
      this.itemval = val;
      this.licenseidarr = [];
      for (let i = 0; i < this.itemval.length; i++) {
        this.licenseidarr.push(this.itemval[i].hash.id);
      }
      // console.log(this.licenseidarr, "23");
    },
    // 监听子组件的(添加)
    addlicense(val) {
      this.Object = {};
      this.Object = val;
      // val.hash.holder = val.hash.holders;
      // console.log(val, "22223322");
      // this.carlist.unshift(val);
      // console.log(this.licenselist, "66666");
    },
    // 公共组件刷新
    reload(partsname) {
      // console.log(partsname);
      this[partsname] = false;
      this.$nextTick(() => {
        this[partsname] = true;
      });
    },
    // 点击添加上岗证信息
    showaddlicense() {
      (this.dialogVisible = true), (this.btnvalue = 1);
      this.reload('isaddlicense');
    },
    // 分页
    handleCurrentChange() {
      this.getlicenselist();
    },
    // 获取人员类别
    getitem1() {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid: '0204',
        codetype: '02',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        if (ts.d.sflag == 'success') {
          this.itemdata1 = ts.d.data.operation1;
          // console.log(this.itemdata1,'888')
        }
      });
    },
    // 获取职业领域
    getitem2() {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid: '0503',
        codetype: '05',
        type: '1',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        if (ts.d.sflag == 'success') {
          this.itemdata2 = ts.d.data.operation1;
          // console.log(this.itemdata2,'888')
        }
      });
    },
    // 获取岗位资格
    getitem3() {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid: '0504',
        codetype: '05',
        type: '2',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        if (ts.d.sflag == 'success') {
          this.itemdata3 = ts.d.data.operation1;
          // console.log(this.itemdata3,'888')
        }
      });
    },
    // 获取列表
    getlicenselist() {
      // console.log("2222");
      if (this.licenseData.hash.starttime == null) {
        this.licenseData.hash.starttime = [];
      }
      if (this.licenseData.hash.validtime == null) {
        this.licenseData.hash.validtime = [];
      }
      const req = {
        handle:
          '1CB43CC1631B5D5B14CBF34BC23B86F231062A3F18BDCAFA39249CF3DA4DD76E44B455B1B2775577',
        username: this.licenseData.hash.username,
        cardnum: this.licenseData.hash.cardnum,
        icnum: this.licenseData.hash.icnum,
        cardtype: this.licenseData.hash.cardtype,
        occupation_type: this.occupation_typeArr,
        position_type: this.position_typeArr,
        starttime1: this.licenseData.hash.starttime[0],
        starttime2: this.licenseData.hash.starttime[1],
        validtime1: this.licenseData.hash.validtime[0],
        validtime2: this.licenseData.hash.validtime[1],
        pid: this.licenseData.hash.pid,
        id: this.licenseData.hash.id,
        start: (this.pagobj.currentPage3 - 1) * this.pagobj.pagesize,
        limit: this.pagobj.pagesize,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.licenselist = ts.d.data.operation1;
        this.pagobj.total = parseInt(ts.d.data.operation2[0].hash.allrs);
        // console.log(this.licenselist,'789')
      });
    }
  },
};
</script>

